/**
 * 插入排序
 */
Array.prototype.insertionSort = function() {
  console.log(this.join(','));

  // j 是 i 前面的元素索引
  let j = 0,
    aux = 0;

  // i 是未排序的元素索引
  for (let i = 1; i < this.length; i++) {
    aux = this[i];
    j = i - 1;

    while (this[j] > aux && j >= 0) {
      this[j + 1] = this[j];
      j--;
    }
    this[j + 1] = aux;
    console.log(this.join(','));
  }
  return this;
};

console.log([5, 4, 3, 2, 1].insertionSort()); // Array(5) [1, 2, 3, 4, 5]
